package com.iweb.controller;


import com.iweb.pojo.Place;
import com.iweb.service.PlaceService;
import com.iweb.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@Controller
@RequestMapping("/place")
public class PlaceController {
    @Autowired
    PlaceService placeService;
    @Autowired
    TypeService typeService;

    @RequestMapping(value = "delete")
    public void deleteAdmin(HttpServletRequest req, HttpServletResponse resp, int placeID) throws IOException {
        PrintWriter out = resp.getWriter();
        if (placeService.delete(placeID)) {
            out.print("success");
        } else {
            out.print("error");
        }
    }

    @RequestMapping("/book")
    public String book(HttpServletResponse resp, HttpServletRequest req, Place place) {
/*
        String userID = req.getSession().getAttribute("userID").toString();
*/
        req.setAttribute("placeID", place.getPlaceID());
        req.setAttribute("placeName", place.getPlaceName());
        req.setAttribute("money", place.getMoney());
        return "user/place/book";
    }

    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public String select(HttpServletResponse resp, HttpServletRequest req, String name) {
        if (name == null){
            name = "";
        }
        if (name.equals("admin")) {
            System.out.println(placeService.selectPlace());
            req.setAttribute("places", placeService.selectPlace());
            return "/admin/ground/index";
        }
        System.out.println(placeService.selectPlace());
        req.setAttribute("places", placeService.selectPlace());
        return "user/place/placeShow";
    }


    @RequestMapping(value = "/addPlace", method = RequestMethod.GET)
    public String addPlace(HttpServletRequest req) {
        List list = typeService.selectAll();
        System.out.println(list);
        req.setAttribute("typeList",list);
        return "/admin/ground/add";

    }

    @RequestMapping(value = "/updatePlace", method = RequestMethod.GET)
    public String updatePlace(HttpServletRequest req) {
        List list = typeService.selectAll();
        System.out.println(list);
        List places = placeService.selectAll();
        req.setAttribute("placeList",places);
        req.setAttribute("typeList",list);
        return "/admin/ground/setting";

    }

    @RequestMapping(value = "/updatePlace", method = RequestMethod.POST)
    public String updatePlace(HttpServletRequest req ,Place place) {
        placeService.updatePlace(place);
        req.setAttribute("places", placeService.selectPlace());
        return "/admin/ground/index";
    }

    @RequestMapping(value = "/addPlace", method = RequestMethod.POST)
    public String addPlace(Place place, HttpServletRequest req) {
        System.out.println(place);
        placeService.addPlace(place);
        req.setAttribute("places", placeService.selectPlace());
        return "/admin/ground/index";

    }
}
